﻿using System.Linq;
using Samples.Radio.Agent;
using Samples.Radio.Data;

namespace Samples.Radio.Pages.Program
{
    public class PodcastPageViewModel:ViewModelBase
    {
        public string SelectedProgramName { get; set; }
        public string SelectedPodcastName { get; set; }

        private ProgramViewModel _CurrentProgram;

        public ProgramViewModel CurrentProgram
        {
            get { return _CurrentProgram; }
            set
            {
                if (CurrentProgram == value) return;
                _CurrentProgram = value;
                RaisePropertyChanged(() => CurrentProgram);
            }
        }

        private PodcastViewModel _SelectedPodcast;

        public PodcastViewModel SelectedPodcast
        {
            get { return _SelectedPodcast; }
            set
            {
                if (SelectedPodcast == value) return;
                _SelectedPodcast = value;
                RaisePropertyChanged(() => SelectedPodcast);
            }
        }



        public PodcastPageViewModel(RadioRepository repository, IDispatcher dispatcher, INavigation navigation)
            : base(repository, dispatcher, navigation)
        {            
            AddEventWiring(() => Repository.LoadProgramsCompleted += LoadProgramsCompleted,
                   () => Repository.LoadProgramsCompleted -= LoadProgramsCompleted);
        }

        private void LoadProgramsCompleted(object sender, ParameterEventArgs<ProgramViewModel[]> e)
        {
            if (e == null || e.Parameter1 == null)
            {
                // TODO: Handle errors in loading program data
                return;
            }


             e.Parameter1.ToList().ForEach(
                p =>
                {
                    p.LoadPodcastsCompleted += (s, loadedPodcasts) =>
                    {
                        var currentPodcast = loadedPodcasts.Parameter1.ToList().FirstOrDefault(pod => pod.Title == SelectedPodcastName);
                        
                        if (currentPodcast != null)
                        {
                            CurrentProgram = p;
                            SelectedProgramName = p.ProgramName;
                            SelectedPodcast = currentPodcast;
                        }
                    
                    };
                }
             );



        }

        public override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs navigationEventArgs, System.Windows.Navigation.NavigationContext context, System.Collections.Generic.IDictionary<string, object> pageState)
        {
            base.OnNavigatedTo(navigationEventArgs, context, pageState);

            SelectedPodcastName = context.QueryString["ProgramName"];

            Repository.LoadPrograms();
        }

        public void AddSelectedPodcast()
        {
            if (SelectedPodcast == null) return;
            Repository.AddPodcastToPlaylist(SelectedPodcast);
            SelectedPodcast = null;
        }
    }
}
